package net.xdclass.forum.service.impl;

import net.xdclass.forum.dao.ReplyDao;
import net.xdclass.forum.domain.Reply;
import net.xdclass.forum.domain.User;
import net.xdclass.forum.dto.PageDTO;
import net.xdclass.forum.service.ReplyService;

import java.util.Date;
import java.util.List;

public class ReplyServiceImpl implements ReplyService {
    ReplyDao replyDao = new ReplyDao();
    @Override
    public PageDTO<Reply> findReply(int topicId, int page, int pageSize) {
        int totalReplyRecord = replyDao.countReplyByTopicId(topicId);
        int from = (page-1) * pageSize;
        List<Reply> replyList = replyDao.findReplyByTopicId(topicId, from, pageSize);
        PageDTO<Reply> pageDTO = new PageDTO<>(page,pageSize,totalReplyRecord);
        pageDTO.setList(replyList);
        return pageDTO;
    }

    @Override
    public int replyByTopicId(User user, int topicId, String content) {
        // 获取最新楼层
        int floor =replyDao.findLatestFloorByTopicId(topicId);
        Reply reply = new Reply();
        reply.setContent(content);
        reply.setCreateTime(new Date());
        reply.setUpdateTime(new Date());
        reply.setDelete(0);
        reply.setFloor(floor+1);
        reply.setTopicId(topicId);
        reply.setUserId(user.getId());
        reply.setUserImg(user.getImg());
        reply.setUsername(user.getUsername());
        int rows = replyDao.replyByTopicId(reply);



        return 0;
    }
}
